package tgc.edu.yzy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tgc.edu.yzy.entity.Bj;
import tgc.edu.yzy.entity.Grade;
import tgc.edu.yzy.entity.Student;
import tgc.edu.yzy.entity.Teacher;
import tgc.edu.yzy.service.TeacherService;
import tgc.edu.yzy.service.BjService;
import tgc.edu.yzy.service.GradeService;
import tgc.edu.yzy.service.StudentService;

@Controller
@RequestMapping(value="/teacher")
public class TeacherController {
	private static final String List = null;
	@Autowired
	private TeacherService teacherservice;
	@Autowired
	private GradeService gradeservice;
	@Autowired
	private BjService bjservice;
	@Autowired
	private StudentService studentservice;
	/***
	 * 教师修改密码
	 * @param password
	 * @param session
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/updatepassword")
	public String updatepassword(String password,HttpSession session,ModelMap map) {
		Teacher teachers=(Teacher) session.getAttribute("teachers");
		teacherservice.updatepassword(password,teachers.getId());
		map.put("password", "教师");
		session.removeAttribute("teachers");
		return "homepage/index";
	}
	/***
	 * 显示教师所带学生的基本信息
	 * 有可能信息过多，所以用page分页显示
	 * @return
	 */
	@RequestMapping(value="required")
	public String required(HttpSession session,ModelMap map,Integer page) {
		Teacher teachers = (Teacher) session.getAttribute("teachers");
		/*List<Grade> grades=gradeservice.findByTeacherId(teachers.getId());*/
		if(page==null) {
			page=0;
		}
		Pageable pageable=PageRequest.of(page, 5);
		Page<Grade> grades=gradeservice.findByTeacherId(teachers.getId(),pageable);
		int a1=grades.getNumber();//第几页数据
		int a2=grades.getNumberOfElements();//这页的数据
		long a3=grades.getTotalElements();//总共多少条数据
		int a4=grades.getTotalPages();//总共多少页
		int a5=a4;
		String sql = "";
		for (int i = 0; i < a5; i++) {
			sql+="<li><a href='required?page="+i+"'>"+(i+1)+"</a></li>";
		}
		map.put("sql", sql);
		map.put("a1", "第"+(a1+1)+"页");
		map.put("a2", "显示数据"+a2+"条");
		map.put("a3", "总共"+a4+"页&emsp;共"+a3+"条数据");
		map.put("grades", grades.getContent());
		return "teacher/students";
	}
	/***
	 * 修改学生信息
	 * @return
	 */
	@RequestMapping(value="studentmanage")
	public String studentmanage(HttpSession session,ModelMap map,Integer page) {
		List<Bj> bjs=bjservice.findAll();
		map.put("bj", bjs);
		return "teacher/studentmanage";
	}
	@RequestMapping(value="findall")
	public String findall(Integer page,Integer bjid,ModelMap map,HttpSession session) {
		if(page==null) {
			page=0;
		}
		
		Integer pageyzy = (Integer) session.getAttribute("yaozhenyong");
		Integer bjidyzy=(Integer) session.getAttribute("yaozhenyong1");
		if(pageyzy!=null) {
			page=pageyzy;
		}
		if(bjidyzy!=null) {
			bjid=bjidyzy;
		}
		session.removeAttribute("yaozhenyong");
		session.removeAttribute("yaozhenyong1");
		
		Teacher teacher = (Teacher) session.getAttribute("teachers");
		Integer teacherids=teacher.getId();
		Pageable pageable=PageRequest.of(page, 10);
		if(bjid==null||bjid==-1) {
			Page<Grade> student=gradeservice.findByTeacherId(teacherids,pageable);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='pages("+i+")'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "teacher/student_index";
		}else{
			Page<Grade> student=gradeservice.findByTeacherId(teacherids,pageable);
			map.put("bjid", bjid);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='return pages1("+i+","+bjid+");'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "teacher/student_index";
		}
	}
	/***
	 * 
	 * @param grade 成绩
	 * @param id 	Grade表 ID
	 * @param page	分页对象
	 * @param bjid	Bjid
	 * @return
	 */
	@RequestMapping(value="grandes",method=RequestMethod.POST)
	public String grandes(Integer grade,Integer id,Integer page,Integer bjid,HttpSession session) {
		if(bjid!=null) {
			session.setAttribute("yaozhenyong", page);
		}
		session.setAttribute("yaozhenyong1", bjid);
		Grade qweqwe=gradeservice.findById(id);
		qweqwe.setGrade(grade);
		gradeservice.save(qweqwe);
		return "redirect:findall";
	}
}
